package com.elitetyc.customview.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.elitetyc.customview.R;
import com.elitetyc.customview.model.DayBean;
import com.elitetyc.customview.model.MonthBean;

import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;
import java.util.TreeMap;

/**
 * Created by elitetyc on 2019/6/22.
 * Describe:
 */
public class DoubleCheckMonthAdpater extends BaseQuickAdapter<MonthBean, BaseViewHolder> {

    private DayBean startDay,stopDay;

    private Context mContext;

    public void setmContext(Context mContext) {
        this.mContext = mContext;
    }

    public DoubleCheckMonthAdpater(int layoutResId, @Nullable List<MonthBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, MonthBean item) {
        helper.setText(R.id.tv_month_title,item.year+"年"+item.month+"月");
        RecyclerView rvDay = helper.getView(R.id.rv_day);
        rvDay.setLayoutManager(new GridLayoutManager(mContext,7));
        DoubleCheckDayAdapter doubleCheckDayAdapter = new DoubleCheckDayAdapter(R.layout.view_day,item.dayBeanList);
        rvDay.setAdapter(doubleCheckDayAdapter);
        if (helper.getLayoutPosition()==getData().size()){
            helper.getView(R.id.v_bottom_line).setVisibility(View.GONE);
        }

    }

    private class DoubleCheckDayAdapter extends BaseQuickAdapter<DayBean,BaseViewHolder>{

        public DoubleCheckDayAdapter(int layoutResId, @Nullable List<DayBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, DayBean item) {
            TextView textView = helper.getView(R.id.tv_day_item);
            if (item.day==0){
                textView.setText("");
                textView.setEnabled(false);
            }else {
                textView.setText(String.valueOf(item.day));
                textView.setEnabled(true);
                if (!item.canSelect){
//                    如果不能被选中，设置背景为灰色
                    textView.setEnabled(false);
                    switch (item.lrm) {
                        case 1:
                            textView.setBackgroundResource(R.drawable.shape_day_bg_gray_1);
                            break;
                        case 2:
                            textView.setBackgroundResource(R.drawable.shape_day_bg_gray_2);
                            break;
                        case 3:
                            textView.setBackgroundResource(R.drawable.shape_day_bg_gray_3);
                            break;
                        case 4:
                            textView.setBackgroundResource(R.drawable.shape_day_bg_gray_4);
                            break;
                        default:
                            break;
                    }
                }else {
                    textView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
//                            点击事件
                            if (startDay==null){
//                                说明这是第一次点击，直接设置开始时间为当前点击的时间
                                startDay = new DayBean(item.year,item.month,item.day,item.lrm);
                                item.isSelect = true;
                                item.lrm = caculateStart(item);
                            }else if (startDay!=null&&stopDay==null){
//                                说明再选择结束时间
                                if (item.year==startDay.year){
                                    if (item.month==startDay.month){
                                        if (item.day>startDay.day){
                                            stopDay = new DayBean(item.year,item.month,item.day,item.lrm);
                                            setOtherSelected();
                                            item.lrm = caculateStop(item);
                                            item.isSelect= true;
                                        }else{
                                            setAllNotSelected();
                                            startDay = new DayBean(item.year,item.month,item.day,item.lrm);
                                            item.lrm = caculateStart(item);
                                            item.isSelect = true;
                                        }
                                    }else if (item.month>startDay.month){
                                        stopDay = new DayBean(item.year,item.month,item.day,item.lrm);
                                        setOtherSelected();
                                        item.lrm = caculateStop(item);
                                        item.isSelect= true;
                                    }else{
                                        setAllNotSelected();
                                        startDay = new DayBean(item.year,item.month,item.day,item.lrm);
                                        item.lrm = caculateStart(item);
                                        item.isSelect = true;
                                    }
                                }else if(item.year>startDay.year){
                                    stopDay = new DayBean(item.year,item.month,item.day,item.lrm);
                                    setOtherSelected();
                                    item.lrm = caculateStop(item);
                                    item.isSelect= true;
                                } else {
                                    setAllNotSelected();
                                    startDay = new DayBean(item.year,item.month,item.day,item.lrm);
                                    item.lrm = caculateStart(item);
                                    item.isSelect = true;
                                }
                            }else if (startDay!=null&&stopDay!=null){
                                setAllNotSelected();
                                startDay = new DayBean(item.year,item.month,item.day,item.lrm);
                                item.lrm = caculateStart(item);
                                item.isSelect = true;

                            }
                            if (onDateCheckLister!=null){
                                onDateCheckLister.onDateCheckedLister(startDay,stopDay);
                            }
                            DoubleCheckMonthAdpater.this.notifyDataSetChanged();
                            notifyDataSetChanged();

                        }
                    });
                    if (item.isSelect){
//                    如果是选中了的，设置背景颜色和文字颜色
                        textView.setTextColor(Color.WHITE);

                        switch (item.lrm){
                            case 1:
                                textView.setBackgroundResource(R.drawable.shape_day_bg_1);
                                break;
                            case 2:
                                textView.setBackgroundResource(R.drawable.shape_day_bg_2);
                                break;
                            case 3:
                                textView.setBackgroundResource(R.drawable.shape_day_bg_3);
                                break;
                            case 4:
                                textView.setBackgroundResource(R.drawable.shape_day_bg_4);
                                break;
                                default:
                                    break;
                        }
                    }
                }

            }

        }
    }

    public interface OnDateCheckLister{
        void onDateCheckedLister(DayBean startDay,DayBean stopDay);
    }

    private OnDateCheckLister onDateCheckLister;


    public OnDateCheckLister getOnDateCheckLister() {
        return onDateCheckLister;
    }

    public void setOnDateCheckLister(OnDateCheckLister onDateCheckLister) {
        this.onDateCheckLister = onDateCheckLister;
    }

    private int caculateStart(DayBean dayBean){
        Calendar calendar = Calendar.getInstance();
        calendar.set(dayBean.year,dayBean.month-1,dayBean.day);
        if (calendar.get(Calendar.DAY_OF_WEEK)==7){
            return 2;
        }else {
            return 1;
        }
    }

    private int caculateStop(DayBean dayBean){
        Calendar calendar = Calendar.getInstance();
        calendar.set(dayBean.year,dayBean.month-1,dayBean.day);
        if (calendar.get(Calendar.DAY_OF_MONTH)==1){
            return 2;
        }else if (calendar.get(Calendar.DAY_OF_WEEK)==1){
            return 2;
        }else {
            return 3;
        }
    }

    private void setOtherSelected() {

        Calendar startTime = Calendar.getInstance();
        startTime.set(startDay.year,startDay.month-1,startDay.day);
        long sTime = startTime.getTimeInMillis();
        Calendar stopTime = Calendar.getInstance();
        stopTime.set(stopDay.year,stopDay.month-1,stopDay.day);
        long eTime = stopTime.getTimeInMillis();
        for (MonthBean monthBean:getData()){
            for (DayBean dayBean:monthBean.dayBeanList){
                if (dayBean.day!=0){
                    Calendar dayTime = Calendar.getInstance();
                    dayTime.set(dayBean.year,dayBean.month-1,dayBean.day);
                    long dTime = dayTime.getTimeInMillis();
                    if (dTime>=sTime&&dTime<=eTime){
                        dayBean.isSelect = true;
                    }
                }
            }
        }

    }

    private void setAllNotSelected() {
        for (MonthBean monthBean:getData()){
            for (DayBean dayBean:monthBean.dayBeanList){
                if (dayBean.day!=0){
                    if (startDay!=null){
                        if (dayBean.year == startDay.year&&dayBean.month==startDay.month&&dayBean.day==startDay.day){
                            dayBean.lrm = startDay.lrm;
                        }
                    }

                    if (stopDay!=null){
                        if (dayBean.year == stopDay.year&&dayBean.month==stopDay.month&&dayBean.day==stopDay.day){
                            dayBean.lrm = stopDay.lrm;
                        }
                    }

                }
            }
        }
        startDay = null;
        stopDay = null;
        for (MonthBean monthBean:getData()){
            for (DayBean dayBean:monthBean.dayBeanList){
                dayBean.isSelect = false;
            }
        }
    }
}
